﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Argotic.Common;
using System.Xml.XPath;
using Argotic.Data.Adapters;
using Argotic.Syndication;
using HtmlAgilityPack;
using System.Text.RegularExpressions;

namespace MyPublish.Connection.Syndication
{
    public class RssFeedContext
    {
        public  const string DEFAULT_RSS_TITLE = "rss";
        public string RssUrl { get; set; }
        public string RssTitle { get; set; }

        public static RssFeedContext Create(string rssUrl, string title)
        {
            return new RssFeedContext(rssUrl, title);
        }
        public static RssFeedContext Create(string rssUrl)
        {
            return new RssFeedContext(rssUrl);
        }

        public RssFeedContext(string rssUrl)
            :this(rssUrl,DEFAULT_RSS_TITLE)
        {
        }

        public RssFeedContext(string rssUrl, string title)
        {
            this.RssUrl = rssUrl;
            this.RssTitle = title;
        }

        public IEnumerable<PostData> Posts
        {
            get
            {
                List<PostData> items = new List<PostData>();
                RssFeed feed = new RssFeed();
                SyndicationResourceLoadSettings setting = new SyndicationResourceLoadSettings()
                {
                    AutoDetectExtensions = true,
                    CharacterEncoding = Encoding.UTF8,
                    RetrievalLimit = 2000
                };

                DiscoverableSyndicationEndpoint endpoint = null;
                try
                {
                    Uri uri = new Uri(RssUrl);
                    endpoint = new DiscoverableSyndicationEndpoint(uri, "application/rss+xml", RssTitle);
                }
                catch (Exception e)
                {
                    throw new DataConnectionException("Failed to create endpoint " + RssUrl, e);
                }

                XPathNavigator navigator = null;
                try { navigator = endpoint.CreateNavigator(); }
                catch { }
                if (navigator == null)
                {
                    return items;
                }

                try
                {
                    SyndicationResourceAdapter adapter =
                        new SyndicationResourceAdapter(navigator, setting);
                    adapter.Fill(feed, SyndicationContentFormat.Rss);

                    foreach (RssItem entry in feed.Channel.Items)
                    {
                        PostData item = new PostData()
                        {
                            Title = new PostText()
                            {
                                Text = entry.Title,
                                Type = PostTextType.Text
                            },
                            Published = entry.PublicationDate,
                            Content = new PostContent()
                            {
                                Type = PostTextType.Text.ToString(),
                                Content = entry.Description
                            },
                            Link = entry.Link
                        };
                        items.Add(item);
                    }

                    return items;
                }
                catch (Exception e)
                {
                    throw new DataConnectionException("Failed to query " + RssUrl, e);
                }
            }
        }
    }
}
